package com.mmall.service;

import com.google.common.base.Preconditions;
import com.mmall.beans.PageQuery;
import com.mmall.beans.PageResult;
import com.mmall.common.RequestHolde;
import com.mmall.dao.SysUserMapper;
import com.mmall.exception.ParamException;
import com.mmall.model.SysUser;
import com.mmall.param.UserParam;
import com.mmall.util.BeanValidator;
import com.mmall.util.IpUtil;
import com.mmall.util.MD5Util;
import com.mmall.util.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SysUserService {


    @Autowired
    private SysUserMapper sysUserMapper;



    public void save (UserParam param){
        BeanValidator.check(param);
         if (checkTelephoneExist(param.getTelephone(),param.getId())){
             throw new ParamException("电话已被占用");
             }
             if (checkEmailExist(param.getMail(),param.getId())){

                  throw new ParamException("邮箱已被占用");
             }
             String password = PasswordUtil.randomPassword();
             password = "12345678";
        String encryptedPassword = MD5Util.encrypt(password);

        SysUser user = SysUser.builder()
                .username(param.getUsername())
                .telephone(param.getTelephone())
                .mail(param.getMail())
                .password(encryptedPassword)
                .deptId(param.getDeptId())
                .status(param.getStatus())
                .remark(param.getRemark())
                .build();


        user.setOperator(RequestHolde.getCurrentUser().getUsername());//TODO
        user.setOperateIp(IpUtil.getRemoteIp(RequestHolde.getCurrentRequest()));
        user.setOperateTime(new Date());

        //TODO


        sysUserMapper.insertSelective(user);

    }

    public void update(UserParam param) {
        BeanValidator.check(param);
        if(checkTelephoneExist(param.getTelephone(), param.getId())) {
            throw new ParamException("电话已被占用");
        }
        if(checkEmailExist(param.getMail(), param.getId())) {
            throw new ParamException("邮箱已被占用");
        }
        SysUser before = sysUserMapper.selectByPrimaryKey(param.getId());
        Preconditions.checkNotNull(before, "待更新的用户不存在");
        SysUser after = SysUser.builder()
                .id(param.getId())
                .username(param.getUsername())
                .telephone(param.getTelephone())
                .mail(param.getMail())
                .deptId(param.getDeptId())
                .status(param.getStatus())
                .remark(param.getRemark())
                .build();

        after.setOperator(RequestHolde.getCurrentUser().getUsername());
        after.setOperateIp(IpUtil.getRemoteIp(RequestHolde.getCurrentRequest()));
        after.setOperateTime(new Date());


        sysUserMapper.updateByPrimaryKeySelective(after);


    }

    public boolean checkEmailExist(String mail, Integer userId) {

       return sysUserMapper.countByMail(mail,userId)>0;

    }

    public boolean checkTelephoneExist(String telephone, Integer userId) {

        return sysUserMapper.countByTelephone(telephone, userId) > 0;
    }

    public SysUser findByKeyword(String keyword) {
        return sysUserMapper.findByKeyword(keyword);
    }
    public PageResult<SysUser> getPageByDeptId(int deptId, PageQuery page) {

        BeanValidator.check(page);
        int count = sysUserMapper.countByDeptId(deptId);
        if (count > 0){

            List<SysUser>list = sysUserMapper.getPageByDeptId(deptId, page);
            return PageResult.<SysUser>builder().total(count)
                    .data(list).build();

        }
        return PageResult.<SysUser>builder().build();

    }
    public List<SysUser>getAll(){

        return null;
    }







}
